package com.zwb.demo.easyweb.modules.menu;

import com.zwb.demo.easyweb.common.entity.response.CommonCode;
import com.zwb.demo.easyweb.common.entity.response.ResponseResult;
import com.zwb.demo.easyweb.core.exception.ExceptionCast;
import com.zwb.demo.easyweb.core.utils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 系统菜单表
 *
 * @author zwb
 * @date 2020-04-30T11:20:54.324554900
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    private final SysMenuPORepository sysMenuRepository;

    @Autowired
    public SysMenuServiceImpl(SysMenuPORepository sysMenuRepository) {
        this.sysMenuRepository = sysMenuRepository;
    }


    @Override
    public ResponseResult saveOne(SysMenuVO vo) {
        if (StringUtils.isNotBlank(vo.getId())){
            Optional<SysMenuPO> optional = sysMenuRepository.findById(vo.getId());
            if (!optional.isPresent()){
                ExceptionCast.cast(CommonCode.ID_NO_EXITS);
            }
            SysMenuPO po = optional.get();
            if (!po.getParentId().equals(vo.getParentId())){
                changeParent(vo.getParentId());
            }
            BeanUtils.copyProperties(vo, po);
            changeParentId(po);
            sysMenuRepository.save(po);
        }else {
            SysMenuPO po = ConvertUtils.convertObject(vo, SysMenuPO.class);
            changeParentId(po);
            po.setIsParent(false);
            sysMenuRepository.save(po);
        }
        return ResponseResult.ok();
    }

    public void changeParent(String parentId){
        if (parentId.equals("0")){
            return;
        }else {
            List<SysMenuPO> allByParentId = sysMenuRepository.findAllByParentId(parentId);
            if (allByParentId.size()<=1){
                Optional<SysMenuPO> optional = sysMenuRepository.findById(parentId);
                if (optional.isPresent()){
                    SysMenuPO sysMenuPO = optional.get();
                    sysMenuPO.setIsParent(false);
                    sysMenuRepository.save(sysMenuPO);
                }
            }
        }
    }

    public void changeParentId(SysMenuPO po){
        if (po.getParentId().equals("0")){
            po.setIsRoot(true);
        }else {
            Optional<SysMenuPO> optional = sysMenuRepository.findById(po.getParentId());
            if (optional.isPresent()){
                SysMenuPO sysMenuPO = optional.get();
                sysMenuPO.setIsParent(true);
                sysMenuRepository.save(sysMenuPO);
            }
            po.setIsRoot(false);
        }

    }

    @Override
    public ResponseResult deleteOne(String uuid) {
        if (StringUtils.isBlank(uuid)){
            ExceptionCast.cast(CommonCode.ILLEGAL_PARAM);
        }
        sysMenuRepository.deleteById(uuid);
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult findAllRootMenu() {
        List<SysMenuPO> allRoot = sysMenuRepository.findAllRoot();
        List<SysMenuVO> allVO = ConvertUtils.convertList(allRoot, SysMenuVO.class);
        return ResponseResult.ok().body(allVO);
    }

    @Override
    public ResponseResult findAllMenuByRoot(String uuid) {
        List<SysMenuVO> allPO = findAllMenuByParentId(uuid);
        List<SysMenuVO> allVO = ConvertUtils.convertList(allPO, SysMenuVO.class);
        return ResponseResult.ok().body(allVO);
    }

    public List<SysMenuVO> findAllMenuByParentId(String uuid) {
        List<SysMenuPO> allChild = sysMenuRepository.findAllByParentId(uuid);
        List<SysMenuVO> allVO = ConvertUtils.convertList(allChild, SysMenuVO.class);
        for (SysMenuVO sysMenuVO : allVO) {
            if (sysMenuVO.getIsParent()){
                List<SysMenuVO> temp = findAllMenuByParentId(sysMenuVO.getId());
                sysMenuVO.setChildren(temp);
            }
        }
        return allVO;
    }

    @Override
    public ResponseResult findAll() {
        List<SysMenuPO> allPO = sysMenuRepository.findAll();
        List<SysMenuVO> allVO = ConvertUtils.convertList(allPO, SysMenuVO.class);
        return ResponseResult.ok().body(allVO);
    }
}
